#include <iostream>
#include "Dealer.h"

using namespace std;

Dealer::Dealer()
{
	this->_Hand = Hand();
}

Dealer::Dealer(Hand hand)
{
	this->_Hand = hand;
}

Hand Dealer::getHand()
{
	return this->_Hand;
}

void Dealer::setHand(Hand hand)
{
	this->_Hand = hand;
}

void Dealer::addCard(Card card)
{
	this->_Hand.AddCard(card);
}

void Dealer::Sort()
{
	this->_Hand.Sort();
}

int Dealer::Evaluate()
{
	int numChange = 0;
	int index1, index2, index3;
	Card card1, card2, card3;
	vector<int> indexList = vector<int>();
	switch(this->_Hand.CountPoint())
	{
	case 10: // Royal Flush
		break;
	case 9: // Straight Flush
		break;
	case 8: // Four of a kind
		index1 = this->_Hand.getIndexOfFourKind();
		if (index1 == 0)
			index2 = 4;
		else
			index2 = 0;
		if (index2 <= 8)
		{
			indexList.push_back(index2);
			numChange = 1;
		}
		break;
	case 7: // Full house
		break;
	case 6: // Flush
		break;
	case 5: // Straight
		break;
	case 4: // Three of a kind
		index1 = this->_Hand.getIndexOfThreeKind();
		if (index1 == 0)
		{
			index2 = 3;
			index3 = 4;
		}
		else if (index1 == 1)
		{
			index2 = 0;
			index3 = 4;
		}
		else
		{
			index2 = 0;
			index3 = 1;
		}
		card1 = this->_Hand.getCardByIndex(index1);
		card2 = this->_Hand.getCardByIndex(index2);
		card3 = this->_Hand.getCardByIndex(index3);
		if (card2.GetFace() > 8) {}
		else if (card3.GetFace() <= 8)
		{
			indexList.push_back(index2);
			indexList.push_back(index3);
			numChange = 2;
		}
		else
		{
			indexList.push_back(index2);
			numChange = 1;
		}
		break;
	case 3: // Two pairs
		index1 = this->_Hand.getIndexOfFirstPair();
		index2 = this->_Hand.getIndexOfSecondPair();
		index3 = 8 - index1 * 2 - index2 * 2;
		card1 = this->_Hand.getCardByIndex(index1);
		card2 = this->_Hand.getCardByIndex(index2);
		card3 = this->_Hand.getCardByIndex(index3);
		if (card1.GetFace() > 8)
		{
			if (card3.GetFace() >= 8)
			{
				indexList.push_back(index3);
				numChange = 1;
			}
		}
		else
		{
			indexList.push_back(index1);
			indexList.push_back(index1 + 1);
			indexList.push_back(index3);
			numChange = 3;
		}
		break;
	case 2: // One pair
		switch (this->_Hand.GetPair1Pos())
		{
		case 0:
			index1 = 2;
			index2 = 3;
			index3 = 4;
			break;
		case 1:
			index1 = 0;
			index2 = 3;
			index3 = 4;
			break;
		case 2:
			index1 = 0;
			index2 = 1;
			index3 = 4;
			break;
		case 3:
			index1 = 0;
			index2 = 1;
			index3 = 2;
			break;
		}
		card1 = this->_Hand.getCardByIndex(index1);
		card2 = this->_Hand.getCardByIndex(index2);
		card3 = this->_Hand.getCardByIndex(index3);
		if (card1.GetFace() < 8)
		{
			numChange++;
			indexList.push_back(index1);
		}
		if (card2.GetFace() < 8)
		{
			numChange++;
			indexList.push_back(index2);
		}
		if (card3.GetFace() < 8)
		{
			numChange++;
			indexList.push_back(index3);
		}
		break;
	case 1: // High card
		indexList.push_back(0);
		indexList.push_back(1);
		indexList.push_back(2);
		numChange = 3;
		break;
	}
	this->_Hand.RemoveCards(indexList);
	return numChange;
}

string Dealer::toString()
{
	string str = "";
	str += "Dealer:\n" + this->_Hand.toString() + "\n";
	return str;
}

int Dealer::comparePlayer(Player player)
{
	return this->_Hand.CompareTo(player.getHand());
}